package com.smart.support.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import com.smart.common.entities.ajax.AjaxResult;
import com.smart.common.entities.constant.SmartConstant;
import com.smart.common.entities.domain.AclMeta;
import com.smart.manager.system.entities.views.SimpleAccount;
import com.smart.support.bodys.ChangePswBody;
import com.smart.support.service.AccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author Xiaxuqiu
 * @description 作用描述
 * @date 2023-04-01
 **/
@Api(tags = "个人中心")
@RestController
@Log4j2
@RequestMapping(SmartConstant.MANAGER_API_PREFIX + "access/account")
@RequiredArgsConstructor
public class AccountApi {

    private final AccountService accountService;

    @ApiOperation(value = "注销登录")
    @PostMapping("logout")
    @SaCheckLogin
    public AjaxResult<String> logout() {
        if (StpUtil.isLogin()) {
            StpUtil.logout();
        }

        return AjaxResult.success();
    }

    @ApiOperation(value = "获取账号简单信息")
    @PostMapping("getSimpleInfo")
    @SaCheckLogin
    public AjaxResult<SimpleAccount> getInfo() {
        SimpleAccount simpleAccount = accountService.getSimpleAccount();
        return AjaxResult.data(simpleAccount);
    }

    @ApiOperation(value = "查询账号ACL资源信息")
    @PostMapping("getAclMeta")
    @SaCheckLogin
    public AjaxResult<AclMeta> getAclMeta() {
        AclMeta aclMeta = accountService.getAccountAcl();
        accountService.fillMenuList(aclMeta);
        aclMeta.setDataScope(null);
        return AjaxResult.data(aclMeta);
    }



    @ApiOperation(value = "修改账号密码")
    @PutMapping("changePsw")
    @SaCheckLogin
    public AjaxResult<String> changePsw(@RequestBody @Validated ChangePswBody body) {
        accountService.changePsw(body) ;
        return AjaxResult.success();
    }

}
